package com.bestcem.xm.om.grpc.service.converter;

import com.bestcem.xm.common.core.uitls.GrpcDateUtil;
import com.bestcem.xm.om.grpc.service.param.OmPlatformParam;
import com.bestcem.xm.om.grpc.v1.services.ListPlatformsRequest;
import com.bestcem.xm.om.grpc.v1.services.Platform;
import com.bestcem.xm.om.service.dto.PlatformDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * grpc请求出入参转化类
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/9 10:11
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OmPlatformConverter {
    /**
     * grpc请求入参转换成本地入参
     *
     * @param request
     * @return com.bestcem.xm.om.entity.OmPlatform
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/6/23 16:02
     */
    public static OmPlatformParam convertToOmPlatform(ListPlatformsRequest request) {
        OmPlatformParam param = new OmPlatformParam();
        param.setId(request.hasId() ? request.getId() : null);
        param.setPlatformCode(request.hasCode() ? request.getCode() : null);
        param.setPlatformName(request.hasName() ? request.getName() : null);
        param.setPlatformUrl(request.hasUrl() ? request.getUrl() : null);
        param.setOrgId(request.hasOrgId() ? request.getOrgId() : null);
        param.setSource(request.hasSource() ? request.getSourceValue() : null);
        param.setLinkName(request.hasLinkName() ? request.getLinkName() : null);
        param.setLinkPhone(request.hasLinkPhone() ? request.getLinkPhone() : null);
        param.setLinkEmail(request.hasLinkEmail() ? request.getLinkEmail() : null);
        param.setSupporter(request.hasSupporter() ? request.getSupporter() : null);
        param.setExpireTime(request.hasExpireTime() ? GrpcDateUtil.getLocalDateTime(request.getExpireTime()) : null);
        param.setAesKey(request.hasAesKey() ? request.getAesKey() : null);
        param.setContactsName(request.hasContactsName() ? request.getContactsName() : null);
        param.setContactsPhone(request.hasContactsPhone() ? request.getContactsPhone() : null);
        param.setContactsEmail(request.hasContactsEmail() ? request.getContactsEmail() : null);
        param.setContactsQrcode(request.hasContactsQrcode() ? request.getContactsQrcode() : null);
        param.setStatuses(request.getStatusesValueList());
        return param;
    }

    /**
     * 将本地处理结果转换成grpc请求响应参数
     *
     * @param data
     * @return com.bestcem.xm.om.grpc.v1.services.Platform
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/6/21 16:29
     */
    public static Platform dtoToResponse(PlatformDTO data) {
        Platform.Builder builder = Platform.newBuilder();
        if (Objects.nonNull(data)) {
            builder.setId(data.getId());
            if (Objects.nonNull(data.getPlatformCode())) {
                builder.setCode(data.getPlatformCode());
            }
            if (Objects.nonNull(data.getPlatformName())) {
                builder.setName(data.getPlatformName());
            }
            if (Objects.nonNull(data.getPlatformUrl())) {
                builder.setUrl(data.getPlatformUrl());
            }
            if (Objects.nonNull(data.getOrgId())) {
                builder.setOrgId(data.getOrgId());
            }
            if (Objects.nonNull(data.getSource())) {
                builder.setSourceValue(data.getSource());
            }
            if (Objects.nonNull(data.getLinkName())) {
                builder.setLinkName(data.getLinkName());
            }
            if (Objects.nonNull(data.getLinkPhone())) {
                builder.setLinkPhone(data.getLinkPhone());
            }
            if (Objects.nonNull(data.getLinkEmail())) {
                builder.setLinkEmail(data.getLinkEmail());
            }
            if (Objects.nonNull(data.getSupporter())) {
                builder.setSupporter(data.getSupporter());
            }
            if (Objects.nonNull(data.getExpireTime())) {
                builder.setExpireTime(GrpcDateUtil.getTimestamp(data.getExpireTime()));
            }
            if (Objects.nonNull(data.getAesKey())) {
                builder.setAesKey(data.getAesKey());
            }
            if (Objects.nonNull(data.getContactsName())) {
                builder.setContactsName(data.getContactsName());
            }
            if (Objects.nonNull(data.getContactsPhone())) {
                builder.setContactsPhone(data.getContactsPhone());
            }
            if (Objects.nonNull(data.getContactsEmail())) {
                builder.setContactsEmail(data.getContactsEmail());
            }
            if (Objects.nonNull(data.getContactsQrcode())) {
                builder.setContactsQrcode(data.getContactsQrcode());
            }
            if (Objects.nonNull(data.getStatus())) {
                builder.setStatusValue(data.getStatus());
            }
            if (Objects.nonNull(data.getSampleCode())) {
                builder.setSampleCode(data.getSampleCode());
            }
        }
        return builder.build();
    }
}
